module.exports = {
  chainWebpack: config => {
    config.module
      .rule('pdfjs')
      .test(/node_modules[\\/]pdfjs-dist[\\/].*\.js$/)
      .use('babel-loader')
      .loader('babel-loader')
      .options({
        plugins: [
          ['@babel/plugin-transform-private-methods', { loose: true }],
          ['@babel/plugin-transform-class-properties', { loose: true }]
        ]
      })
      .end()
  },
  publicPath: '/admin/',
  transpileDependencies: ['pdfjs-dist'],
  devServer: {
    allowedHosts: 'all',
    proxy: {
      '/edumanage': {
        target: 'https://www.originxr.cn/api', // 后端服务地址
        changeOrigin: true,               // 是否更改请求头中的 origin
        secure: true,
        preserveHostHeader: true, // ✅ 关键！保留原始 Host 头
        bypass(req, res, options) {
          const { pathRewrite } = options;
          let rewrittenUrl = req.url;
          // 手动应用 pathRewrite 规则
          if (pathRewrite && typeof pathRewrite === 'object') {
            for (const [pattern, replacement] of Object.entries(pathRewrite)) {
              if (typeof pattern === 'string' && typeof replacement === 'string') {
                rewrittenUrl = rewrittenUrl.replace(new RegExp(pattern), replacement);
              }
            }
          }
          // 构造完整代理 URL
          const proxyUrl = new URL(rewrittenUrl, options.target).href;
          // 添加响应头
          res.setHeader('x-req-proxyUrl', proxyUrl);
        },
      },
      '/api': {
        target: 'http://leight.theoriginvr.tech:9526', // 后端服务地址
        changeOrigin: true,               // 是否更改请求头中的 origin
        secure: true,
        preserveHostHeader: true, // ✅ 关键！保留原始 Host 头
        bypass(req, res, options) {
          const { pathRewrite } = options;
          let rewrittenUrl = req.url;
          // 手动应用 pathRewrite 规则
          if (pathRewrite && typeof pathRewrite === 'object') {
            for (const [pattern, replacement] of Object.entries(pathRewrite)) {
              if (typeof pattern === 'string' && typeof replacement === 'string') {
                rewrittenUrl = rewrittenUrl.replace(new RegExp(pattern), replacement);
              }
            }
          }
          // 构造完整代理 URL
          const proxyUrl = new URL(rewrittenUrl, options.target).href;
          // 添加响应头
          res.setHeader('x-req-proxyUrl', proxyUrl);
        },
      }
    },
    // 可选：自定义 host 和 port
    host: 'localhost',
    port: 8080, // 前端开发服务器端口
  }
}